<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 style="text-align: center;">登录动漫平台</h2>
      <el-form :model="form" ref="loginForm" label-width="70px" >
        <el-form-item label="用户名">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item class="center-btn">
          <el-button type="primary" @click="handleLogin" class="center-btn">登录</el-button>
        </el-form-item>
      </el-form>
      <p style="text-align: center; margin-top: 10px;">
        <router-link to="/register">没有账号？去注册</router-link>
      </p>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { login } from '../api/user'
import { useRouter } from 'vue-router'

const router = useRouter()
const form = ref({ username: '', password: '' })

async function handleLogin() {
  try {
    const res = await login(form.value)
    localStorage.setItem('token', res.data.data) // 保存 JWT
    ElMessage.success('登录成功')
    router.push('/home')
  } catch (err) {
    ElMessage.error('登录失败，请检查用户名和密码')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('../assets/login-bg.jpg') no-repeat center center fixed;
  background-size: cover;
  backdrop-filter: blur(2px);
}
.login-card {
  width: 400px;
  background-color: rgba(255, 255, 255, 0.9);
}
.center-btn .el-form-item__content {
  justify-content: center;
}
</style>
